package com.course.${module}.controller.admin;

import com.course.server.domain.${Domain};
import com.course.server.dto.${Domain}DTO;
import com.course.server.dto.PageDTO;
import com.course.server.dto.ResponseDTO;
import com.course.server.service.${Domain}Service;
import com.course.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/${domain}")
public class ${Domain}Controller {

    private static final Logger logger = LoggerFactory.getLogger(${Domain}Controller.class);

    public static final String BUSINESS_NAME = "${tableNameCn}";

    @Resource
    private ${Domain}Service ${domain}Service;

    @PostMapping("/${domain}")
    public String ${domain}(){
        return "success";
    }

    @PostMapping("/list")
    public ResponseDTO list(@RequestBody PageDTO pageDTO){
        logger.info("pageDTO:{}", pageDTO);
        ResponseDTO responseDTO = new ResponseDTO();
        ${domain}Service.list(pageDTO);
        responseDTO.setContent(pageDTO);
        return responseDTO;
    }

    @PostMapping("/save")
    public ResponseDTO save(@RequestBody ${Domain}DTO ${domain}DTO){
        logger.info("${domain}DTO:{}", ${domain}DTO);

        //保存校验
        <#list fieldList as field>
        <#if field.name!="id" && field.nameHump!="createdAt" && field.nameHump!="updatedAt" && field.nameHump!="sort">
            <#if !field.nullAble>
        ValidatorUtil.require(${domain}DTO.get${field.nameBigHump}(), "${field.nameCn}");
            </#if>
            <#if (field.length > 0)>
        ValidatorUtil.length(${domain}DTO.get${field.nameBigHump}(), "${field.nameCn}", 1, ${field.length?c});
            </#if>
        </#if>
        </#list>


        ResponseDTO responseDTO = new ResponseDTO<>();
        ${domain}Service.save(${domain}DTO);
        responseDTO.setContent(${domain}DTO);
        return responseDTO;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDTO save(@PathVariable String id){
        logger.info("id:{}", id);
        ResponseDTO responseDTO = new ResponseDTO<>();
        ${domain}Service.delete(id);
        return responseDTO;
    }
}
